package com.longg.dao;

import com.longg.comm.Comm;
import com.longg.dao.base.BaseDao;
import com.longg.model.Article;
import com.longg.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.management.Query;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class ArticleDao extends BaseDao<Article> {
    public ArticleDao() {
        super(Article.class);
    }

    public List<Map<String, Object>> findArticleByPage(int currentPage,String name) {
        String where = "";
        if (null != name && !"".equals(name)){
            where = " and title like ? ";
        }
        String sql = "select a.*,ui.real_name,(select count(*) from favorite where a_id = a.id) fnum \n" +
                "from article a,user_info ui\n" +
                "where a.user_id = ui.user_id" + where + "\n" +
                "limit ?,?";
        List<Map<String, Object>> query = null;
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if(null != name && !"".equals(name)){
                query = queryRunner.query(sql,new MapListHandler(),"%" + name + "%",(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }else{
                query = queryRunner.query(sql,new MapListHandler(),(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    public long getCount(String name) {
        String where = "";
        long count = 0;
        if (null != name && !"".equals(name)){
            where = " where title like ? ";
        }
        String sql = "select count(*) from article" + where;
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null != name && !"".equals(name)){
                count = queryRunner.query(sql,new ScalarHandler<Long>(),"%" + name + "%");
            }else {
                count = queryRunner.query(sql,new ScalarHandler<Long>());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public List<Map<String, Object>> favouriteArticle(int id, int currentPage, String name) {
        String where = "";
        if (null != name && !"".equals(name)){
            where = " and title like ? ";
        }
        List<Map<String, Object>> query = null;
        String sql = "select a.*,ui.real_name,(select count(*) from favorite where a_id = a.id) fnum\n" +
                "from article a,user_info ui,favorite f\n" +
                "where f.u_id = ? and a.id = f.a_id and a.user_id = ui.user_id" + where + " \n" +
                "limit ?,?";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null != name && !"".equals(name)){
                query = queryRunner.query(sql,new MapListHandler(),id,"%" + name + "%",(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }else {
                query = queryRunner.query(sql,new MapListHandler(),id,(currentPage - 1) * Comm.PAGE_SIZE,Comm.PAGE_SIZE);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return query;
    }

    public long getFCount(int id, String name) {
        String where = "";
        if (null != name && !"".equals(name)){
            where = " and title like ? ";
        }
        String sql = "select count(*) from favorite f,article a\n" +
                "where f.u_id = ? and f.a_id = a.id" + where;
        long count = 0;
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        try {
            if (null != name && !"".equals(name)){
                count = queryRunner.query(sql,new ScalarHandler<Long>(),id,"%" + name + "%");
            }else {
                count = queryRunner.query(sql,new ScalarHandler<Long>(),id);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public Map<String,Object> findArticleDetail(int id){
        String sql = "select a.*,ui.real_name,(select f.id from favorite f where f.a_id = a.id and f.u_id = ui.user_id) ifFav \n" +
                "from article a,user_info ui\n" +
                "where a.id = ? and a.user_id = ui.user_id";
        QueryRunner queryRunner = new QueryRunner(JDBCUtil.getDataSource());
        Map<String,Object> articleMap = null;
        try {
            articleMap = queryRunner.query(sql,new MapHandler(),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return articleMap;
    }
}
